<?php
/**
 * Created by PhpStorm.
 * User: liuxiaojie
 * Date: 2019-02-24
 * Time: 13:21
 */

namespace app\api\controller;

use app\common\Base;
use app\model\Customer;
use app\tools\MIniprogram;
use app\tools\RedisCache;
use think\Request;

class Company extends Base{

    public $wechat = [];

    function __construct(Request $request = null)
    {
        parent::__construct($request);
    }
    // 通过手机验证码登录
    function loginByMobile(){
        $code = $this->getParam('code', '');
        $mobile = $this->getParam('mobile', '');
        $form_id = $this->getParam('form_id', '');
        $openid = $this->getParam('openid', '');

        $where = [
            'mobile'=> $mobile,
            'status'=> ['<>', 5]
        ];

        $info = db('company')->where($where)->field('id, account, name')->find();
        if(!$info){
            return fail("ACCOUNT_NOT_EXIST");
        }

        \app\tools\Sms::checkSms($mobile, $code);

        if($form_id){
            db('form_data')->insert([
                'form_id'=> $form_id,
                'openid'=> $openid,
                'company_id'=> $info['id'],
                'create_time'=> time(),
            ]);
        }

        RedisCache::getInstance()->hMSet("company_info_".$info['id'], $info);
//        RedisCache::getInstance()->expire("company_info_".$info['id'], 518400);
        return success($info);
    }
    // 通过账号密码登录
    function loginByAccount(){
        $account = $this->getParam('account', '');
        $pass = $this->getParam('pass', '');
        $form_id = $this->getParam('form_id', '');
        $openid = $this->getParam('openid', '');

        $where = [
            'account'=> $account,
            'status'=> ['<>', 5]
        ];

        $info = db('company')->where($where)->field('id, pass, name, account')->find();

        if(!$info){
            return fail("ACCOUNT_NOT_EXIST");
        }

        if($info['pass'] != sign($pass)){
            return fail("PASS_NOT_RIGHT");
        }

        unset($info['pass']);

        if($form_id){
            db('form_data')->insert([
                'form_id'=> $form_id,
                'openid'=> $openid,
                'company_id'=> $info['id'],
                'create_time'=> time(),
            ]);
        }

        RedisCache::getInstance()->hMSet("company_info_".$info['id'], $info);
        RedisCache::getInstance()->expire("company_info_".$info['id'], 518400);
        return success($info);
    }

    // 注册账号
    function register(){
        $mobile = $this->getParam('mobile', '');
        $account = $this->getParam('account', '');
        $code = $this->getParam('code', '');
        $pass = $this->getParam('pass', '');

        if(!$mobile || !$account || !$code || !$pass){
            return fail("DATA_ERROR");
        }

        \app\tools\Sms::checkSms($mobile, $code);

         $id = db("company")->where(["mobile"=> $mobile])->whereOr(["account"=> $account])->value('id');
         if($id){
             return fail("IS_REGISTERED");
         }

        $post = [
            "mobile"=> $mobile,
            "account"=> $account,
            "pass"=> sign($pass),
        ];

         $id = \app\model\Company::add_data($post);

         $info = [
             'id'=> $id,
             'account'=> $account,
             'name'=> ''
         ];

        RedisCache::getInstance()->hMSet("company_info_".$id, $info);
        RedisCache::getInstance()->expire("company_info_".$id, 518400);

         return success($info);

    }

    // 设置新密码

    function setPass(){
        $mobile = $this->getParam('mobile', '');
        $code = $this->getParam('code', '');
        $pass = $this->getParam('pass', '');

        if(!$mobile || !$code || !$pass){
            return fail("DATA_ERROR");
        }

        $where = [
            'mobile'=> $mobile,
        ];

        $info = db('company')->where($where)->field('id, account, name')->find();
        if(!$info){
            return fail("ACCOUNT_NOT_EXIST");
        }

        \app\tools\Sms::checkSms($mobile, $code);
        db("company")->where(['mobile'=> $mobile])->update(['pass'=> sign($pass)]);
        return success([]);

    }

}